package com.linksky.thread;

import com.linksky.crawler.Crawler;
import com.linksky.dao.PlayListRepository;
import com.linksky.dao.SongRepository;
import com.linksky.entity.PlayList;
import com.linksky.entity.Song;
import com.linksky.util.DatabaseUtils;
import com.mysql.jdbc.Connection;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class PlayListCrawlerThread extends Thread {

	private Crawler crawler;
	@Autowired
	private PlayListRepository playListRepository;

	@Autowired
	private SongRepository songRepository;

	public PlayListCrawlerThread(Crawler crawler) {
		this.crawler = crawler;
	}
	@Override
	public void run() {
		while (!isInterrupted()) {
			System.out.println(currentThread().getId() + " ---load playlist start---");
			List<PlayList> playLists = crawler.loadNewPlayList();
			for (PlayList playList : playLists) {
				PlayList temp = Crawler.crawlPlayList(playList);
				Connection conn = null;
				try {
					conn = (new DatabaseUtils.Builder().setHost("localhost").setDbName("music163").setUser("study")
							.setPassword("123").builder()).getConection();
					PreparedStatement 	ps = conn.prepareStatement("INSERT into music163.play_list(id,name) values (?,?)");
					 ps.setLong(1, temp.getId());
				     ps.setString(2, temp.getName());
					 ps.executeUpdate();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {
				
					e.printStackTrace();
				}finally{
					try {
						conn.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				
				//System.out.println("++++歌单名+++++" + temp.getId() + ";" + temp.getName());		
				List<Song> songlist = Crawler.crawlSong(playList);
				for(Song song:songlist){
					try {
						conn = (new DatabaseUtils.Builder().setHost("localhost").setDbName("music163").setUser("study")
								.setPassword("123").builder()).getConection();
						PreparedStatement 	ps = conn.prepareStatement("INSERT into music163.song(id,name) values (?,?)");
						 ps.setLong(1, song.getId() );
					     ps.setString(2, song.getName());
						 ps.executeUpdate();
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					} catch (SQLException e) {
					
						e.printStackTrace();
					}finally{
						try {
							conn.close();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
					System.out.println("++++歌名+++++" + song.getId() + ";" + song.getName());
				}
				System.out.println(currentThread().getId() + " ----load playlist end----");
			}

		}
	}
	public void insert(){
		Connection conn = null;
		try {
			conn = (new DatabaseUtils.Builder().setHost("localhost").setDbName("music").setUser("study")
					.setPassword("123").builder()).getConection();
			PreparedStatement 	ps = conn.prepareStatement("INSERT into sps.order(name,phone_num) values (?,?)");
			 ps.setString(1, "张三");
		     ps.setString(2, "13333333333");
			 ps.executeUpdate();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
		
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
}

}